<template>
  <div class="home-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="data-card">
          <template #header>
            <div class="card-header">
              <span>部门总数</span>
            </div>
          </template>
          <div class="card-content">
            <el-statistic :value="statistics.departmentCount">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  部门数量
                </div>
              </template>
            </el-statistic>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="data-card">
          <template #header>
            <div class="card-header">
              <span>员工总数</span>
            </div>
          </template>
          <div class="card-content">
            <el-statistic :value="statistics.employeeCount">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  员工数量
                </div>
              </template>
            </el-statistic>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="data-card">
          <template #header>
            <div class="card-header">
              <span>商品总数</span>
            </div>
          </template>
          <div class="card-content">
            <el-statistic :value="statistics.productCount">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  商品数量
                </div>
              </template>
            </el-statistic>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="data-card">
          <template #header>
            <div class="card-header">
              <span>制造商总数</span>
            </div>
          </template>
          <div class="card-content">
            <el-statistic :value="statistics.manufacturerCount">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  制造商数量
                </div>
              </template>
            </el-statistic>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" style="margin-top: 20px">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近添加的商品</span>
            </div>
          </template>
          <el-table :data="recentProducts" style="width: 100%">
            <el-table-column prop="name" label="商品名称" />
            <el-table-column prop="category" label="类别" />
            <el-table-column prop="price" label="价格">
              <template #default="{ row }">
                {{ row.price }} 元
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近添加的制造商</span>
            </div>
          </template>
          <el-table :data="recentManufacturers" style="width: 100%">
            <el-table-column prop="name" label="制造商名称" />
            <el-table-column prop="contactPerson" label="联系人" />
            <el-table-column prop="phone" label="联系电话" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '../api'

const statistics = ref({
  departmentCount: 0,
  employeeCount: 0,
  productCount: 0,
  manufacturerCount: 0
})

const recentProducts = ref([])
const recentManufacturers = ref([])

const loadData = async () => {
  try {
    const [departments, employees, products, manufacturers] = await Promise.all([
      api.get('/departments'),
      api.get('/employees'),
      api.get('/products'),
      api.get('/manufacturers')
    ])

    statistics.value = {
      departmentCount: departments.length,
      employeeCount: employees.length,
      productCount: products.length,
      manufacturerCount: manufacturers.length
    }

    // 获取最近添加的5个商品
    recentProducts.value = products.slice(-5).reverse()
    // 获取最近添加的5个制造商
    recentManufacturers.value = manufacturers.slice(-5).reverse()
  } catch (error) {
    console.error('加载数据失败:', error)
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.data-card {
  height: 180px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
}
</style> 